#include "ScreenView.h"

ScreenView::ScreenView() :
	_x0(0), _y0(0),
	_width(0), _height(0)
{
	setMode(ScreenMode::Demo);
	setPos(_x0,_y0);
}

ScreenView::ScreenView(int x, int y, int width, int height) :
	_x0(x), _y0(y),
	_width(width), _height(height)
{
	setMode(ScreenMode::Demo);
	setPos(x,y);
}

void ScreenView::setGeometry(int width, int height)
{
	_width = width;//-_x0*2;
	_height = height;//-_y0*2;
	updateView();
}

void ScreenView::setMode(ScreenMode::Enum mode)
{
	switch(mode){
	case ScreenMode::Demo:
		_imageFile = ":/monitor.png";
		_ratio = 1331/1160;
	}
}

void ScreenView::updateView(){
	_ledSize = 30;
	_width -= _ledSize*2;
	_height -= _ledSize*2;
	double sW = _width, sH = _height; // screen sizes
	if((double)_width/_height > _ratio){
		_width = _height*_ratio;
		_x0 = (sW-_width)/2 + _ledSize;
		_y0 = _ledSize;
	}
	else{
		_height = _width/_ratio;
		_y0 = (sH-_height)/2 + _ledSize;
		_x0 = _ledSize;
	}
}

QRectF ScreenView::boundingRect() const
{
	return QRectF(_x0, _y0, _width, _height);
}

QPainterPath ScreenView::shape() const
{
	QPainterPath myPath;
	myPath.addRect(_x0, _y0, _width, _height);

	return myPath;
}

void ScreenView::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
	painter->drawPixmap(_x0, _y0, _width, _height, QPixmap(_imageFile));
}
